package com.qfedu.openplateform.manager.service.impl;

import com.qfedu.openplateform.manager.dao.RechargeMapper;
import com.qfedu.openplateform.manager.entity.Recharge;
import com.qfedu.openplateform.manager.service.RechargeService;
import com.qfedu.openplateform.manager.util.KeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class RechargeServiceImpl implements RechargeService {
    @Autowired
    private RechargeMapper rechargeMapper;

    @Override
    public List<Recharge> findAllRecharge() {
        List<Recharge> rechargeList = rechargeMapper.findAllRecharge();
        return rechargeList;
    }

    @Override
    public int deleteRechargeByIds(List<Integer> ids) {
        if (ids != null) {
            for (Integer id : ids) {
                rechargeMapper.deleteRechargeById(id);
            }
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public int addRecharge(Recharge recharge) {
        Integer cusId = recharge.getId();
        if (cusId == null) {
            recharge.setCusId(0);
            recharge.setId(0);
        } else {
            recharge.setCusId(cusId);
            recharge.setId(0);
        }
        Date date = new Date();
        recharge.setCreatetime(date);
        recharge.setUpdatetime(date);
        String orderIdStr = KeyUtil.genUniqueKey();
        Long orderId = Long.valueOf(orderIdStr);
        recharge.setOrderId(orderId);
        int i = rechargeMapper.addRecharge(recharge);
        return i;
    }

    @Override
    public int updateRecharge(Recharge recharge) {
        Date date = new Date();
        recharge.setUpdatetime(date);
        int i = rechargeMapper.updateRecharge(recharge);
        return i;
    }
}
